package com.fenglingzff.reportstudent.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fenglingzff.reportstudent.common.PageInfo;
import com.fenglingzff.reportstudent.common.ReportInfo;
import com.fenglingzff.reportstudent.common.Result;
import com.fenglingzff.reportstudent.entity.Studata1;
import com.fenglingzff.reportstudent.service.Studata1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzp
 * @since 2024-08-26
 */
@RestController
@RequestMapping("/studata1")
public class Studata1Controller {
    @Autowired
    Studata1Service studata1Service;
    @PostMapping("/page")
    public Result listpage(@RequestBody PageInfo pageInfo){
        Page<Studata1> page = new Page<>();
        HashMap map = pageInfo.getMap();
        LambdaQueryWrapper<Studata1> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        /*专业*/
        String enrollMajor = (String) map.get("enrollMajor");

        /*姓名*/
        String name = (String) map.get("name");
        /*身份证号*/
        String idcard = (String) map.get("idcard");
        /*是否报道*/
        String status = (String) map.get("status");

        /*学院编码*/
        String instituteCode = (String) map.get("instituteCode");

        /*查询学院*/
        if(StringUtils.isNotBlank(instituteCode) && !"null".equals(instituteCode)){
            if(!"7".equals(instituteCode)){
                lambdaQueryWrapper.eq(Studata1::getInstituteCode,instituteCode);
            }
        }
        /*是否报道*/
        if(StringUtils.isNotBlank(status) && !"null".equals(status)){
            if("0".equals(status) || "1".equals(status) || "9".equals(status)){
                lambdaQueryWrapper.eq(Studata1::getStatus,status);
            }
        }

/*是否有查询专业字段*/
        if(StringUtils.isNotBlank(enrollMajor) && !"全部".equals(enrollMajor)){
            lambdaQueryWrapper.like(Studata1::getEnrollMajor,enrollMajor);
        }
/*是否有查询姓名字段*/
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Studata1::getName,name);
        }
/*是否有身份证号码字段*/
        if(StringUtils.isNotBlank(idcard) && !"null".equals(idcard)){
            lambdaQueryWrapper.like(Studata1::getIdCard,idcard);
        }
        page.setCurrent(pageInfo.getPageNum());
        page.setSize(pageInfo.getPageSize());

        Page<Studata1> result = studata1Service.page(page, lambdaQueryWrapper);

        return Result.success(result.getRecords(),result.getTotal());
    }
    /*通过id返回学生信息*/
    @GetMapping("/getById")
    public Result getStudatabyId(String id){
        Studata1 result = studata1Service.getById(id);
        if(result!=null){
            return Result.success(result);
        }
        else{
            return Result.fail();
        }
    }
    /*
    * 通用修改更新数据接口
    * 传递：实体对象修改
    * */
    @PostMapping("/updata")
    public Result mod(@RequestBody Studata1 studata1){
        boolean b = studata1Service.updateById(studata1);
        if (b) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }
    @GetMapping("/cancel")
    public Result mod2(String id){
        UpdateWrapper<Studata1> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("KSH", id).set("isreport", "0");
        boolean result = studata1Service.update(updateWrapper); // 调用 update 方法
        if (result) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    @GetMapping("/getCount")
    public Result getCount(String instituteCode){
        Integer count;
        LambdaQueryWrapper<Studata1> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        LambdaQueryChainWrapper<Studata1> studata1LambdaQueryChainWrapper = studata1Service.lambdaQuery();
        /*学院总人数*/
        if(!"7".equals(instituteCode)){
             count = studata1LambdaQueryChainWrapper.eq(Studata1::getInstituteCode, instituteCode).count();
            lambdaQueryWrapper.eq(Studata1::getInstituteCode, instituteCode).eq(Studata1::getStatus, '1');
        }else{
             count = studata1Service.count();
             lambdaQueryWrapper.eq(Studata1::getStatus, '1');
        }


        /*学院报道人数*/

        Integer haveReport= studata1Service.count(lambdaQueryWrapper);
        /*Integer haveReport = studata1LambdaQueryChainWrapper.eq(Studata1::getStatus, '1').count();*/
        /*学院未报道人数*/
        Integer noReport = count- haveReport;

        /*报道率*/
        double reportRate = (double) haveReport / count * 100;
        String reportRateString = String.format("%.2f%%", reportRate);
        ReportInfo reportInfo = new ReportInfo();
        reportInfo.setCount(count);
        reportInfo.setHaveReport(haveReport);
        reportInfo.setNoReport(noReport);
        reportInfo.setReportRate(reportRateString);
        return Result.success(reportInfo);
    }
    @GetMapping("/getnoe")
    public Result getCount(){
        LambdaQueryWrapper<Studata1> studata1LambdaQueryWrapper = new LambdaQueryWrapper<>();
        studata1LambdaQueryWrapper.eq(Studata1::getStatus,"0");
        studata1LambdaQueryWrapper.eq(Studata1::getInstituteCode,"1");
        Page<Studata1> res = studata1Service.page(new Page<Studata1>().setCurrent(1).setSize(7), studata1LambdaQueryWrapper);
        List<Studata1> list = res.getRecords();
        return Result.success(list);
    }
}
